﻿using System;
using DG.Tweening;
using UnityEngine;

namespace ZyGame.UI
{
    [UIOptions("Resources/MsgBox", UIOptions.UIDepth.MsgBox)]
    internal class MsgBox : UIWindow
    {
        private Action ok;
        private Action cancel;

        public void SetMsgBox(string title, string msg, Action ok, Action cancle)
        {
            this.ok = ok;
            this.cancel = cancle;
            this.SetText("title", title)
                .SetText("info", msg)
                .OnClick("btn_close", OnClose)
                .OnClick("btn_cancel", OnClose)
                .OnClick("btn_entry", OnEntry);
        }

        private void OnEntry()
        {
            this.GetChild("ani").transform.DOScale(Vector3.zero, 0.3f).SetEase(Ease.InBack).onComplete = () =>
            {
                ok?.Invoke();
                Client.Window.CloseView<MsgBox>();
            };
        }

        private void OnClose()
        {
            this.GetChild("ani").transform.DOScale(Vector3.zero, 0.3f).SetEase(Ease.InBack).onComplete = () =>
            {
                cancel?.Invoke();
                Client.Window.CloseView<MsgBox>();
            };
        }

        public override void Awake(params object[] paramsList)
        {
            base.Awake();
            this.GetChild("ani").transform.localScale = Vector3.zero;
            this.GetChild("ani").transform.DOScale(Vector3.one, 0.3f).SetEase(Ease.OutBack);
        }
    }
}